import type { Effect, Reducer } from 'umi';
import { message } from 'antd';

export type newsState = {
  newsDetail?: {};
};

export type NewsType = {
  namespace: 'news';
  state: newsState;
  effects: {
    fetchNewsDetail: Effect;
  };
  reducers: {
    setNewsDetail: Reducer<newsState>;
  };
};

const NewsModel: NewsType = {
  namespace: 'news',
  state: {
    newsDetail: [],
  },
  effects: {
    *fetchNewsDetail({ payload }, { call, put }) {
      const { news } = payload;
      try {
        yield put({
          type: 'setNewsDetail',
          payload: {
            news,
          },
        });
      } catch (err) {
        message.error('获取新闻详情失败，请联系管理员').then();
      }
    },
  },
  reducers: {
    setNewsDetail(state, action) {
      const { news } = action.payload;
      return {
        ...state,
        newsDetail: news,
      };
    },
  },
};
export default NewsModel;
